﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Common
{
    public class DateHelper
    {
        #region MyRegion
        
        public static DateTime StampToDateTime(string time)
        {
            time = time.Substring(0, 10);
            double timestamp = Convert.ToInt64(time);
            System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
            dateTime = dateTime.AddSeconds(timestamp).ToLocalTime();
            return dateTime;
        }

        public static string TimeSubTract(DateTime time1, DateTime time2)
        {
            TimeSpan subTract = time1.Subtract(time2);
            return $"{subTract.Days} 天 {subTract.Hours} 时 {subTract.Minutes} 分 ";
        }
        /// <summary>
        ///  时间戳转本地时间-时间戳精确到秒
        /// </summary> 
        public static DateTime ToLocalTimeDateBySeconds(long unix)
        {
            var dto = DateTimeOffset.FromUnixTimeSeconds(unix);
            return dto.ToLocalTime().DateTime;
        }

        /// <summary>
        ///  时间转时间戳Unix-时间戳精确到秒
        /// </summary> 
        public static long ToUnixTimestampBySeconds(DateTime dt)
        {
            DateTimeOffset dto = new DateTimeOffset(dt);
            return dto.ToUnixTimeSeconds();
        }

        /// <summary>
        ///  时间戳转本地时间-时间戳精确到毫秒
        /// </summary> 
        public static DateTime ToLocalTimeDateByMilliseconds(long unix)
        {
            var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix);
            return dto.ToLocalTime().DateTime;
        }

        /// <summary>
        ///  时间转时间戳Unix-时间戳精确到毫秒
        /// </summary> 
        public static long ToUnixTimestampByMilliseconds(DateTime dt)
        {
            DateTimeOffset dto = new DateTimeOffset(dt);
            return dto.ToUnixTimeMilliseconds();
        }

        public static int CompareOnlyDate(DateTime dateTime1, DateTime dateTime2)
        {
            string d1 = dateTime1.ToShortDateString();
            string d2 = dateTime2.ToShortDateString();
            DateTime t1 = Convert.ToDateTime(d1);
            DateTime t2 = Convert.ToDateTime(d2);
            return DateTime.Compare(t1, t2);
        }

        /// <summary>
        /// 只比较时间部分，如果有DateTime 参与，需要先手动 DateTime.TimeofDay
        /// </summary>
        /// <param name="timeSpan1"></param>
        /// <param name="timeSpan2"></param>
        /// <returns></returns>
        public static int CompareOnlyTimePart(TimeSpan timeSpan1, TimeSpan timeSpan2)
        {
            TimeSpan sp1 = new TimeSpan(timeSpan1.Hours, timeSpan1.Minutes, timeSpan1.Seconds);
            TimeSpan sp2 = new TimeSpan(timeSpan2.Hours, timeSpan2.Minutes, timeSpan2.Seconds);
            return TimeSpan.Compare(sp1, sp2);
        }

        #endregion

        #region 时间转换

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp(System.DateTime time)
        {
            long ts = ConvertDateTimeToInt(time);
            return ts.ToString();
        }
        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public static long ConvertDateTimeToInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            return t;
        }
        /// <summary>        
        /// 时间戳转为C#格式时间        
        /// </summary>        
        /// <param name=”timeStamp”></param>        
        /// <returns></returns>        
        public static DateTime ConvertStringToDateTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        #endregion

        #region 转换格式

        /// <summary>
        /// 时间字符串转时间
        /// </summary>
        /// <param name="timeStr"></param>
        /// <returns></returns>
        public static DateTime Parse(string timeStr)
        {
            DateTime result;
            try
            {
                if (timeStr.Length == 14)
                {
                    result = DateTime.ParseExact(timeStr, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
                }
                else
                {
                    if (timeStr.Length == 8)
                    {
                        result = DateTime.ParseExact(timeStr, "yyyyMMdd", CultureInfo.CurrentCulture);
                    }
                    else
                    {
                        result = DateTime.Now;
                    }
                }
            }
            catch
            {
                result = DateTime.Now;
            }
            return result;
        }

        /// <summary>
        /// 时间字符串转时间格式
        /// </summary>
        /// <param name="date">时间字符串</param>
        /// <param name="dateFormat">时间格式</param>
        /// <returns></returns>
        public static System.DateTime StrToDate(string date, string dateFormat)
        {
            System.DateTime time;
            if (dateFormat != null)
            {
                System.DateTime.TryParseExact(date, dateFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out time);
            }
            else
            {
                try
                {
                    time = System.Convert.ToDateTime(date);
                }
                catch (System.Exception)
                {
                    time = System.DateTime.MinValue;
                }
            }
            return time;
        }
        /// <summary>
        /// 比较两个日期，返回dateEnd-dateStart相差天数
        /// </summary>
        /// <param name="dateStart">开始时间</param>
        /// <param name="dateEnd">结束时间</param>
        /// <returns></returns>
        public static int DateDiff(DateTime dateStart, DateTime dateEnd)
        {
            DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
            DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());
            TimeSpan sp = end.Subtract(start);
            return sp.Days;
        }

        #endregion
    }
}
